<?PHP
	require_once('./misc.php');
		global $db, $yap, $jackknife;

	$NUM_PER_PAGE = 30;
	$CUR_PAGE = $_SERVER['PHP_SELF'];
	$page = (isset($_REQUEST['page']))?$_REQUEST['page']:1;
	
	if ( isset( $_GET['usr'] ) && isset( $_GET['api'] ) ){
		addAPI($_GET['usr'],$_GET['api']);
		error("Successfully added api key", "
		The following details have been added to database.<br />
		UserID:{$_GET['usr']}<br />
		APIKey:{$_GET['api']}
		");
	}
	
	if ( isset( $_GET['del'] ) && is_numeric( $_GET['del'] )){
		if ( isset ($_GET['y']) ) {
			deactivateAPI($_GET['usr']);
		} else {
		error("Delete Api Key", "
					Are you sure you want to delete the following UserID and Api key from DB? ( <b>{$_GET['del']}</b> )
				</div>
				<div class='aRight'> 
				<a href='?p=apimgr&del={$_GET['del']}&y'>Yes</a>
				/ 
				<a href='?p=apimgr'>No</a>");
		}
	}
	
	$query_api_count = mysql_query('SELECT * FROM api_details WHERE active=1', $jackknife);
	$total_api = mysql_num_rows($query_api_count);
	$total_pages = ceil(($total_api/$NUM_PER_PAGE));
	
	$query_all_api = mysql_query("SELECT * FROM api_details WHERE active=1 ORDER BY usr LIMIT ".(($page*$NUM_PER_PAGE)-$NUM_PER_PAGE).",".$NUM_PER_PAGE, $jackknife);
	echo "<table width='100%'>";
	echo "<tr><td colspan=4>
	<form action='{$CUR_PAGE}'> 
		<input name='p' type='hidden' value='{$p}'> SEARCH : 
		<input type='text' name='usr' onfocus='if(this.value=='USERID')api.value='';usr.value='';' value='USERID' /> 
		<input type='text' name='api' size=75 onfocus='if(this.value=='API KEY')api.value='';usr.value='';' value='API KEY' /> 
		<input type='submit' value='Search' /> 
	</form>
	</td>";
	$a = 'normal';	
	while($data = mysql_fetch_assoc($query_all_api)) {
		
		try {
			$ale = AleFactory::getEVEOnline();
			$ale->setCredentials($data['usr'], $data['api']);
			
			$fullApi = true;
			try { $status = $ale->account->AccountStatus();
			} catch (Exception $e) { if ($e->getCode() == 200) { $fullApi=false; } }

			try {echo $chars = $ale->account->Characters();
			} catch (Exception $e) { echo "<b>",$e->getMessage(),"</b>";deactivateAPI($data['usr']); }
			
			
			$count=1;
			foreach ($chars->result->characters as $character) 
				$count++;
		#if ($count < 4) 
			$count = (5-$count);
		#echo $count;
			echo "<tr>";
			foreach ($chars->result->characters as $character) {
				$characterID = (string) $character->characterID;
				$ale->setCharacterID($character->characterID);
				$charName = (string) $character->name;
				$corpName = (string) $character->corporationName;
				$params['corporationID'] = (int) $character->corporationID;
				$corpSheetFull = $ale->corp->CorporationSheet($params)->toArray();;
				$corpSheet = $corpSheetFull['result'];
				$alliance = ( isset( $corpSheet['allianceName'] ) ) ? $corpSheet['allianceName'] : "None";
				
				echo "<td colspan='{$count}'>";
				if (!$fullApi) echo "<s>";
				echo "
					<a href='../jackknife/index.php?userid={$data['usr']}&apik={$data['api']}&charid={$characterID}'> 
						<img src='http://image.eveonline.com/Character/{$characterID}_64.jpg' width=64 height=64 />
						",$charName, "<br />(",$corpName,")", "<br />(",$alliance,")
					<a/> 
					";
				if (!$fullApi) echo "</s>";
				echo "</td>";
				$count = 1;
			}echo "<td><a href='?p=apimgr&del={$data['usr']}'>Delete</a></td>";
			
			
		} catch (Exception $e) { echo
		"
			Error in API<br />
			User ID: ",$data['usr'],"<br />
			API Key: ",$data['api'],"<br />
			Error: (",$e->getCode(),") ",$e->getMessage(),"
		";
		}
	echo "</tr>\n";
	if ($a!="normal") $a="normal"; else $a="qrg";
	}
	echo "</table>";
	$bpage = (($page-1)<1)?1:$page-1;
	$npage = (($page+1)>$total_pages)?$total_pages:$page+1;
	echo "<span> <a href=?p=apimgr&page={$bpage}> Prev</a> Page {$page} of {$total_pages} <a href=?p=apimgr&page={$npage}>Next</a></span><br />Managed By ALE API.";
?>